/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.cells.opreate;

import cn.ac.nercis.pes.community.utils.ControlUtils;
import cn.ac.nercis.pes.community.utils.IconUtils;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableView;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;

import java.util.Objects;

/**
 * 操作列
 * @param <T>
 * @author zpy
 * @version 1.0.0
 */
public class OperateTableCell<T> extends TableCell<T,String> {
    private final ToggleButton editButton = new ToggleButton();
    private final ToggleButton delButton = new ToggleButton();
    private final TableView<T> tableView;

    private EventHandler<? super MouseEvent> editClickEvent;
    private EventHandler<? super MouseEvent> delClickEvent;

    public OperateTableCell(TableView<T> tableView) {
        this(tableView,null,null);
    }

    public OperateTableCell(TableView<T> tableView,
                            EventHandler<? super MouseEvent> editClickEvent,
                            EventHandler<? super MouseEvent> delClickEvent) {
        this.tableView = tableView;
        this.editClickEvent = editClickEvent;
        this.delClickEvent = delClickEvent;
        this.init();
    }

    private void init(){
        editButton.getStyleClass().add("tab-option-btn");
        delButton.getStyleClass().add("tab-option-btn");
        editButton.setGraphic(IconUtils.getGlyph("edit-icon","table-option-icon"));
        delButton.setGraphic(IconUtils.getGlyph("del-icon","table-option-icon"));
        editButton.setTooltip(new Tooltip("编辑"));
        delButton.setTooltip(new Tooltip("删除"));
        editButton.setOnMouseClicked(event -> {
            tableView.getSelectionModel().select(getIndex());
            if(Objects.nonNull(editClickEvent)){
                editClickEvent.handle(event);
            }
        });
        delButton.setOnMouseClicked(event->{
            tableView.getSelectionModel().select(getIndex());
            if(Objects.nonNull(delClickEvent)){
                delClickEvent.handle(event);
            }
        });
    }

    public void setOnEditButtonClick(EventHandler<? super MouseEvent> editClick){
        this.editClickEvent = editClick;
    }

    public void setOnDelButtonClick(EventHandler<? super MouseEvent> delClick){
        this.delClickEvent = delClick;
    }

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setGraphic(null);
        } else {
            HBox hBox = new HBox(editButton,
                    ControlUtils.generateSeparator(Orientation.VERTICAL),
                    delButton);
            hBox.setSpacing(0);
            hBox.setAlignment(Pos.CENTER);
            setGraphic(hBox);
        }
    }
}